#ifndef _TIMERS_H_
#define _TIMERS_H_

#include <sys/time.h>

#ifndef INFTIM
#define INFTIM -1
#endif /* INFTIM */

#ifndef PERIODIC
#define PERIODIC 1
#endif 

#ifndef ONE_SHOT
#define ONE_SHOT 0
#endif

/*回调函数数据定义区*/
typedef union 
{
        void* p;
        int i;
        long l;
} TIMER_DATA;

/*回调函数函数申明*/
typedef void procTimer( TIMER_DATA stTimerData, struct timeval *pstNow );

/* 定时器结构体定义区*/
typedef struct TIMER_STRUCT
{
        procTimer  *pfnProcTimer;
        TIMER_DATA stTimerData;
        long dwMsecs;
        int iPeriodic;
        struct timeval stTime;
        struct TIMER_STRUCT* pstPrev;
        struct TIMER_STRUCT* pstNext;
        int iHash;
} TIMER;

/*定时器列表定义区*/
typedef struct TIMERS_STRUCT
{
        TIMER **pstTimersTable;
        int  iSize;
        int  iAllocCount;
        int iActiveCount;
        int iFreeCount;
        TIMER *pstFreeTimers;
}TIMERS;

/* 初始化定时器列表 */
extern void initTmr( TIMERS * , int );

/*创建一个定时器 */
extern TIMER* createTmr(
                TIMERS *pstTable, struct timeval* pstNow, procTimer* pfnProcTimer, TIMER_DATA stTimerData,
                long dwMsecs, int iPeriodic );

/*定时器列表里下一个定时器的触发间隔*/
extern struct timeval* timeoutTmr(TIMERS *pstTable, struct timeval* pstNow );

/*定时器列表里下一个定时器的触发毫秒数*/
extern long mstimeoutTmr( TIMERS *pstTable, struct timeval* pstNow );

/*运行列表里的各个定时器*/
extern void runTmr(TIMERS *pstTable,  struct timeval* pstNow );

/*重置定时器*/
extern void resetTmr( TIMERS *pstTable, struct timeval* pstNow, TIMER* pstTimer );

/*取消一个定时器，对一次性的定时器无作用*/
extern void cancelTmr( TIMERS *pstTable, TIMER* pstTimer );

/*清除无用的定时器*/
extern void cleanupTmr( TIMERS *pstTable);

/*清除所有的定时器*/
extern void destroyTmr( TIMERS *pstTable);

/* Generate debugging statistics syslog message. */
extern void logStatsTmr( TIMERS *pstTable,long secs );

#endif /* _TIMERS_H_ */
